[Clang][CodeGen] Fix bad codegen when building Clang with latest MSVC #102681
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Before this PR, when using the latest MSVC
Microsoft (R) C/C++ Optimizing Compiler Version 19.40.33813 for x64
one of the Clang unit test used to fail:CodeGenObjC/gnustep2-direct-method.m
, see full failure log here.This seems to have been introduced by c9e5af3 however further inspection shows that commit only triggers a bug in the MSVC compiler.
It seems that the symptom is bad alignement generated in one of the load instructions:
When
Builder.CreateLoad
is called here, somehow this call toAddr.getAlignment().getAsAlign()
returns a bad alignement. The problem occurs at the highlighted line in the screenshot (sub bh,cl
):The code line on the right is translated to the assembly on the left.
llvm::count_zero
returns a proper value (as seen inrcx
), howeversub bh, cl
uses a bad constant inbh
(it is not 63 as expected). I think the optimizer meant to usedil
notbh
. A few lines below it doesmov byte ptr [rsp + 40h], dil
. If aftersub
is executed I manually set 6 inrdi
, as it should have been, the test passes.This happens only in optimized builds. I used
llvm\utils\release\build_llvm_release.bat --version 19.0.0 --x64 --skip-checkout --local-python
to build a LLVM installer.Bug reported here: https://developercommunity.visualstudio.com/t/Bad-code-generation-when-building-LLVM-w/10719589?port=1025&fsid=e572244a-cde7-4d75-a73d-9b8cd94204dd